<%@page import="com.gcxx.framework.util.string.StringUtil"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%@taglib uri="/WEB-INF/tld/frsf.tld" prefix="frsf"%>
<%@include file="/htmlframe/jsp/common_dialog.jsp"%>
<%
	String gatherId =request.getParameter("gatherId");
	String printState = request.getParameter("printState");
	String outStockNo = request.getParameter("outStockNo");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title><s:text name="title"></s:text></title>
<script type="text/javascript" src="<%=basePath%>htmlframe/js/extendEasyui/jeasyui_childDialog.js"></script>
<style type="text/css">
body{padding: 1px; margin: 0px;}
</style>
<script language="javascript" src="<%=hf_jsPath %>/lodop/LodopFuncs.js"></script>
<object  id="LODOP_OB" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0> 
<embed id="LODOP_EM" type="application/x-print-lodop" width=0 height=0></embed>
</object>
</head>
<body class="easyui-layout" style="padding:1px;">

 <div data-options="region:'center',title:'出库汇总',split:true,collapsible:false" style="padding:1px; border-left: 0px;">
 <input type="hidden" id="gatherId" value=""/>
 <input type="hidden" id="printState" value=""/>
 <input type="hidden" id="outStockNo" value=""/>
		<table id="dataTable" ></table>
		<table style="width: 100%;">
			<tr>
				<td align="center" valign="middle">
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-print'" style="width:80px" onclick="do_print()">打印</a>&nbsp;&nbsp;&nbsp;
				    <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" style="width:80px" onclick="closeMenu()">返回</a>&nbsp;&nbsp;&nbsp;
				    <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-print'" style="width:100px" onclick="do_printQrCode()">打印批号码</a>
				</td>
			</tr>
		</table>
 </div>
 <div id="childDialog"></div>
 <script type="text/javascript">
 var basePath= '<%=basePath%>';
 var gatherId = '<%=gatherId%>';
 if(gatherId && gatherId != "" && gatherId != "null"){
 	$("#gatherId").val(gatherId);
 }
 var printState = '<%=printState%>';
 if(printState && printState != "" && printState != "null"){
 	$("#printState").val(printState);
 }
 var outStockNo = '<%=outStockNo%>';
 if(outStockNo && outStockNo != "" && outStockNo != "null"){
 	$("#outStockNo").val(outStockNo);
 }

 var unitArray=[];
 //初始化加载
 $(function(){
	initGetUnitObj();//获取物资单位数组
	initTable();//渲染dataGrid
 });
 //初始化数据区
 function initTable(){
	var height=hheight-75;
	$('#dataTable').datagrid({
		width:'100%',
		height:height,
		nowrap: false,
		autoRowHeight: true,
		striped: true,
		collapsible:true,
		url:basePath + 'goods/goodsIoCenOutd_getPrintOutStockDetail.do',
		singleSelect:true,
		columns:[[
			{field:'id',checkbox:true},
			{title:'出库单号',field:'outStockNo',width:'120',formatter:function(value,row,index){
				if(value && value !=""){
 					return '<span style="height:40px;display:block;vertical-align:middle;line-height:40px;">'+value+'</span>';
				}else{
					return '<span style="height:40px;display:block;vertical-align:middle;line-height:40px;"></span>';
				}
			}},
			{title:'品种',field:'goodsName',width:'150'},
			{title:'规格',field:'goodsSpecification',width:'100'},
			{title:'生产厂商',field:'manufacturerName',width:'150'},
			{title:'供应商物资编号',field:'supplierGoodsNo',hidden:true},
			
			{title:'单位',field:'stockUnitName',width:'70'},
			{title:'数量',field:'outQty',width:'50'},
			{title:'单价',field:'stockPrice',width:'100',formatter:function(value,row,index){
				return parseFloat(value).toFixed(2);
			}},
			{title:'单品总金额',field:'totalMoney',width:'100',formatter:function(value,row,index){
				if(row.outQty && row.stockPrice){
					return parseFloat(row.stockPrice*row.outQty).toFixed(2);
				}else{
					return 0.00;
				}
			}},
			{title:'批号',field:'batchNo',width:'100'},
			{title:'效期',field:'validate',width:'100'},
			{title:'消毒批号',field:'disBatchNo',width:'100'},
			{title:'消毒效期',field:'disValidate',width:'100'},
			{title:'发票号',field:'invoiceNo',width:'100'},
			{title:'大包装',field:'unit1Name',width:'70'},
			{title:'大、中转换率',field:'u1ToU2',width:'80'},
			
			{title:'中包装',field:'unit2Name',width:'70'},
			{title:'中、小转换率',field:'u2ToU3',width:'80'},
			
			{title:'小包装',field:'unit3Name',width:'70'},
			{title:'医院编号',field:'hospitalNo',hidden:true},
			{title:'科室编号',field:'applyOrgNo',hidden:true},
			{title:'科室',field:'applyOrgName',hidden:true},
			{title:'供应商编号',field:'supplierNo',hidden:true},
			{title:'采购单号',field:'purNo',hidden:true},
			{title:'送货地址编号',field:'daddressHospitalNo',hidden:true},
			{title:'送货地址',field:'daddressHospital',hidden:true},
			{title:'中心送货地址编号',field:'daddressCenterNo',hidden:true},
			{title:'中心送货地址',field:'daddressCenter',hidden:true},
			{title:'预计到货时间',field:'planeInDate',hidden:true},
			{title:'品种编号',field:'goodsNo',hidden:true},
			{title:'是否追踪',field:'goodsValueType',hidden:true},
			{title:'生产厂商',field:'manufacturerNo',hidden:true},
			{title:'his供应商编号',field:'hisSupplierNo',hidden:true},
			{title:'his供应商',field:'hisSupplierName',hidden:true},
			{title:'出货部门编号',field:'outOrgNo',hidden:true},
			{title:'出货部门',field:'outOrgName',hidden:true}

		]],
		queryParams:{//当请求远程数据时，发送的额外参数 
			'gatherId':$("#gatherId").val(),
			'printState':$("#printState").val(),
			'outStockNo':$("#outStockNo").val()
		},
		pagination:false,
		rownumbers:true
	});
}



//获取物资单位的数组 [{"unitCode":"1","unitName":"单位1"},{"unitCode":"2","unitName":"单位2"}]
function initGetUnitObj(){
	$.ajax({
		type : "POST",
		dataType : "json",
		async : false,
		url : basePath + '/shareInfo_getGoodsUnitJson.do',
		traditional : false,//在struts2下该属性必须有 
		success : function(result) {
			if (result && result != "") {
				var data = eval('(result)');
				unitArray=data;
			}
		}
	});
}
//创建select下拉框
function CreateUnitSelectHtml(field,value,index,unitArray,isDisabled){
	var selectHtml = "";
	if(isDisabled && isDisabled == true){
		selectHtml='<select id="'+field+"_"+index+'"  disabled="disabled">';
	}else{
		selectHtml='<select id="'+field+"_"+index+'">';
	}
	selectHtml += '<option value="">请选择</option>';
	for (var i = 0; i < unitArray.length; i++) {
		var unitObj=unitArray[i];
		if(value == unitObj.unitCode)
		selectHtml += '<option value="'+unitObj.unitCode+'" selected="selected" >'+unitObj.unitName+'</option>';
		else
		selectHtml += '<option value="'+unitObj.unitCode+'">'+unitObj.unitName+'</option>';
	}
	selectHtml +='</select>';
	return selectHtml;
}
//打印
function do_print(){
	var flag = false;//控制是否打印，若查询成功，开始打印
	var rows=[];//打印的行数据数组
	var notOutStockGoods=[];//未能完成出货的行记录
 	var userName="";//打印人
 	var printTime="";//打印时间
// 	var daddressHospital="";//打印送货地址
	var gatherId = $("#gatherId").val();
	var printState = $("#printState").val();
	var outStockNo = $("#outStockNo").val();
	if(!gatherId || gatherId == ""){
		alert("不能获取出货单号，不能打印");
		return false;
	}
	$.ajax({
		type : "POST",
		dataType : "json",
		async : false,
		url : basePath + 'goods/goodsIoCenOutd_getPrintData.do',
		data:{
			'gatherId':gatherId,
			'printState':printState,
			'outStockNo':outStockNo
		},
		traditional : false,//在struts2下该属性必须有 
		success : function(result) {
			if (result && result != "") {
				var dataObj = eval('(result)');
				if(dataObj.success == "yes"){
					notOutStockGoods = dataObj.notOutStock;
					rows = dataObj.rows;
					userName=dataObj.userName;
					printTime=dataObj.printTime;
					outStockNo=dataObj.outStockNo;
					flag=true;
				}else{
					alert("查询出库数据失败！");
				}
			}else{
				alert("查询出库数据失败！");
			}
		}
	});
	if(flag){//可以打印
		var canPrint=false;//存在未出库数据，用户选择决定是否允许打印
		//判断rows是否为空
		if(!rows || rows.length<1){
			alert("没有查询到打印数据，请核实！");
			return false;
		}
		//判断出库单中是否存在未出库品种
		if(notOutStockGoods && notOutStockGoods.length >0){
			var alertStr="";
			for (var i = 0; i < notOutStockGoods.length; i++) {
				var goodsObj= notOutStockGoods[i];
				alertStr += "【"+goodsObj.goodsName+"】";
			}
			if(alertStr != ""){
				if(confirm((alertStr+"\n出货单中以上品种没有完成出库，是否确定打印出库单？"))){
					canPrint=true;
				}
			}
		}else{
			canPrint= true;
		}
		
		if(canPrint){
		//允许打印
		//打印的数据都在rows数组里面
			var pageHeader = "<table border='0' cellSpacing='0' cellPadding='0'  style='width:100%;'>" +
				"<tr><td width='100%' align='center' ><font size=5>出库单明细</font></td></tr>"+
			"</table>" +
			"<table border='0' cellSpacing='0' cellPadding='0'  style='width:100%;'><tr><td width='20%' valign='top' style='padding:3px 0px;'>随货单号:" + outStockNo + 
				"</td><td width='20%' valign='top' style='padding:3px 0px;'>请领单位:"+rows[0].stockName+
				"</td><td width='20%' valign='top' style='padding:3px 0px;'>送货地址:"+rows[0].daddressHospital+
				"</td><td width='40%' valign='top'>&nbsp;</td></tr>" +
			"<tr><td width='20%' valign='top' style='padding:3px 0px;'>出库单位:"+rows[0].supplierName+
				"</td><td width='20%' valign='top' style='padding:3px 0px;'>制单人:" + userName + 
				"</td><td width='20%' valign='top' style='padding:3px 0px;'>制单日期:" + printTime + 
				"</td><td width='40%' valign='top'>&nbsp;</td></tr>" + 
			"</table>";
			var LODOP =getLodop();
			LODOP.PRINT_INIT("打印出库明细");
			//LODOP.SET_PRINT_PAGESIZE(2,"210mm","297mm","210mm*297mmPage");
			LODOP.SET_PRINT_PAGESIZE(2,0,0,"A4");
			LODOP.SET_SHOW_MODE("LANDSCAPE_DEFROTATED",1);
			printInfo(LODOP, outStockNo,pageHeader,rows);
			LODOP.PREVIEW();

			//打印结束以后，父页面关闭子页面，并刷新数据列表，更新打印状态
			top.closeDialog();
			top.refreshDataGrid();
		}
		
	}
}
 
//根据对象获得每页打印数据
	function printInfo(LODOP, barcode,pageHeader, pageBody){
		if(pageBody){
		//打印出库每页显示信息行数
		var set_PrintRowNum = 10;
		//后台获取 打印出库每页显示信息行数
		$.ajax({
			type : "POST",
			dataType : "json",
			async : false,
			url : basePath + '/shareInfo_getPrintPageRows.do',
			traditional : false,//在struts2下该属性必须有 
			success : function(result) {
				if(result && result !=""){
					var data=eval('(result)');
					if(data.pageRows && data.pageRows !=""){
						set_PrintRowNum = parseInt(data.pageRows);
					}
				}
			}
		});
		//当前出库的物品总数
		var totalNum = pageBody.length;
		//总页数
		var pageSize = totalNum%set_PrintRowNum==0?parseInt(totalNum/set_PrintRowNum):parseInt(totalNum/set_PrintRowNum)+1;
// 		alert("pageSize" + pageSize);
		//循环的当前页数
		var pageNum = 0;
		//循环时记录每页出库物品数量
		var pageRowNum = 0;
		//一页的出库物品信息集合
		var table_body = '';
		if(pageBody != null && pageBody.length > 0){
			for(var i = 0; i < pageBody.length; i++){
				pageRowNum++;
				table_body += "<tr style='border:1px solid #000;'>" + 
				"<td style='font-size:12px;text-align:center;word-wrap:break-word;border:1px solid #000;'>" + (i+1) + "</td>" + 
				"<td style='font-size:12px;text-align:left;word-wrap:break-word;border:1px solid #000;'>" + (pageBody[i].goodsName?pageBody[i].goodsName:'') + "</td>" + 
				"<td style='font-size:12px;text-align:center;word-wrap:break-word;border:1px solid #000;'>" + (pageBody[i].goodsSpecification?pageBody[i].goodsSpecification:'') + "</td>" +
				"<td style='font-size:12px;text-align:center;word-wrap:break-word;border:1px solid #000;'>" + (pageBody[i].outQty?pageBody[i].outQty:'')+ "</td>" + 
				"<td style='font-size:12px;text-align:center;word-wrap:break-word;border:1px solid #000;'>" + (pageBody[i].stockUnitName?pageBody[i].stockUnitName:'') + "</td>" + 
				"<td style='font-size:12px;text-align:center;word-wrap:break-word;border:1px solid #000;'>" + (pageBody[i].batchNo?pageBody[i].batchNo:'') + "</td>" + 
				"<td style='font-size:12px;text-align:center;word-wrap:break-word;border:1px solid #000;'>" + (pageBody[i].validate?pageBody[i].validate:'') + "</td>" +
	 			"<td style='font-size:12px;text-align:center;word-wrap:break-word;border:1px solid #000;'>"+ (pageBody[i].invoiceNo?pageBody[i].invoiceNo:'') + "</td>" + 
	 			"<td style='font-size:12px;text-align:center;word-wrap:break-word;border:1px solid #000;'>" + (pageBody[i].stockPrice?pageBody[i].stockPrice:'') + "</td>" + 
	 			"<td style='font-size:12px;text-align:center;word-wrap:break-word;border:1px solid #000;'>" + ((pageBody[i].stockPrice && pageBody[i].outQty)?(pageBody[i].stockPrice*pageBody[i].outQty):'0') + "</td>" + 
	 			"<td style='font-size:12px;text-align:left;word-wrap:break-word;border:1px solid #000;'>" + (pageBody[i].manufacturerName?pageBody[i].manufacturerName:'') + "</td>" + 
// 	 			"<td style='font-size:12px;text-align:center;word-wrap:break-word;border:1px solid #000;'>" + (pageBody[i].unit1Name?pageBody[i].unit1Name:'') + "</td>" + 
// 	 			"<td style='font-size:12px;text-align:center;word-wrap:break-word;border:1px solid #000;'>" + (pageBody[i].u1ToU2?pageBody[i].u1ToU2:'')+ "</td>" +
// 	 			"<td style='font-size:12px;text-align:center;word-wrap:break-word;border:1px solid #000;'>" + (pageBody[i].unit2Name?pageBody[i].unit2Name:'') + "</td>" + 
// 	 			"<td style='font-size:12px;text-align:center;word-wrap:break-word;border:1px solid #000;'>" + (pageBody[i].u2ToU3?pageBody[i].u2ToU3:'') + "</td>" + 
// 	 			"<td style='font-size:12px;text-align:center;word-wrap:break-word;border:1px solid #000;'>" + (pageBody[i].unit3Name?pageBody[i].unit3Name:'') + "</td>" + 
	 			"</tr>";
	 			
				if((totalNum < set_PrintRowNum && pageRowNum == totalNum) 
						|| (pageRowNum >= set_PrintRowNum && (pageNum <= pageSize - 1)) 
						|| (i + 1) == pageBody.length){
					pageNum++;
					//如果每页行数大于分页数，则将pageRowNum赋值为0
					pageRowNum = 0;
					//如果是新页需要打印基本信息
					LODOP.ADD_PRINT_HTML("0mm","8mm","100%","35mm",pageHeader);
					var table_header = "<table cellSpacing='0' cellPadding='2' style='width:100%;table-layout:fixed;border:1px solid #000;'><tr>" + 
										"<td style='font-size:12px;text-align:center;width:10mm;word-wrap:break-word;border:1px solid #000;'>序号</td>" + 
										"<td style='font-size:12px;text-align:center;width:36mm;word-wrap:break-word;border:1px solid #000;'>品种</td>" + 
										"<td style='font-size:12px;text-align:center;width:38mm;word-wrap:break-word;border:1px solid #000;'>规格</td>" + 
										"<td style='font-size:12px;text-align:center;width:13mm;word-wrap:break-word;border:1px solid #000;'>数量</td>"+
										"<td style='font-size:12px;text-align:center;width:13mm;word-wrap:break-word;border:1px solid #000;'>单位</td>"+
										"<td style='font-size:12px;text-align:center;width:28mm;word-wrap:break-word;border:1px solid #000;'>批号</td>" + 
										"<td style='font-size:12px;text-align:center;width:28mm;word-wrap:break-word;border:1px solid #000;'>效期</td>" + 
//			 							"<td style='font-size:12px;text-align:center;width:18mm;'>消毒批号</td>" + 
//			 							"<td style='font-size:12px;text-align:center;width:18mm;'>消毒效期</td>" + 
										"<td style='font-size:12px;text-align:center;width:18mm;word-wrap:break-word;border:1px solid #000;'>发票号</td>" + 
										"<td style='font-size:12px;text-align:center;width:13mm;word-wrap:break-word;border:1px solid #000;'>单价</td>" + 
										"<td style='font-size:12px;text-align:center;width:18mm;word-wrap:break-word;border:1px solid #000;'>总金额</td>" +
										"<td style='font-size:12px;text-align:center;width:37mm;word-wrap:break-word;border:1px solid #000;'>生产商</td>" + 
// 										"<td style='font-size:12px;text-align:center;width:8mm;word-wrap:break-word;border:1px solid #000;'>大包装单位</td>" + 
// 										"<td style='font-size:12px;text-align:center;width:8mm;word-wrap:break-word;border:1px solid #000;'>大、中包装转换率</td>"+
// 										"<td style='font-size:12px;text-align:center;width:8mm;word-wrap:break-word;border:1px solid #000;'>中包装单位</td>" + 
// 										"<td style='font-size:12px;text-align:center;width:8mm;word-wrap:break-word;border:1px solid #000;'>中、小包装转换率</td>" + 
// 										"<td style='font-size:12px;text-align:center;width:8mm;word-wrap:break-word;border:1px solid #000;'>小包装单位</td>"+
										"</tr>";
					LODOP.ADD_PRINT_HTM("30mm","0mm","100%","293mm", table_header + table_body + '</table>');
					LODOP.ADD_PRINT_BARCODE("8mm","200mm","80mm","15mm","128B",barcode);
			    	LODOP.ADD_PRINT_HTM("0mm","260mm","100%","30mm","第"+pageNum+"页"+"/"+"共"+pageSize+"页");
			    	//如果是新页则需要打印上一页数据
					LODOP.NEWPAGEA();
			    	table_body = '';
				}
			}
		}
//		html += '</table>';
		}
		
//		alert(html);
	}
 function do_cancel(){
		parent.closeDialog();
	}
	
	var oper= oper ||{};
	oper.checkIsNotNull=function(ele,msg){
		var result=true;
		var val=ele.val();
		if(!val || val == ''){
			alert(msg);
			ele.focus();
			result = false;
		}
		return result;
	};
	oper.checkInt=function(ele,msg){
		var result=true;
		var val=ele.val();
		var regx=/^[1-9][0-9]*$/;
		if(!regx.test(val)){
			alert(msg);
			ele.focus();
			result = false;
		}
		return result;
	};
	
function closeMenu(){
	top.closeDialog();
}

//批量生成打印二维条码
function createQRBarCode(options){
	if(options.count && options.count > 0){
		var LOOP=getLodop();
		LOOP.PRINT_INIT("批量打印条码");
//			 
			for(var i = 0; i < options.count; i++){
			LOOP.SET_PRINT_PAGESIZE(1,"80mm","30mm","80*30sPage");
			LOOP.ADD_PRINT_BARCODE("4mm","2mm","25mm","25mm","QRCode",options.goodsNo+"@"+options.batchNo);
			LOOP.ADD_PRINT_HTM("5mm","25mm","50mm","10mm",options.batchNo);
			LOOP.ADD_PRINT_HTM("15mm","25mm","50mm","10mm",options.validate);
			LOOP.NEWPAGE();
			}
		LOOP.PREVIEW();
		}
//	}
//		return returnBarCode;
}

//打印批号码
function do_printQrCode(){
	var row=$('#dataTable').datagrid('getSelected');
	if(!row){
		alert("请选择一条记录！");
	}
	if(confirm("是否确认打印"+row.outQty+"个批号码？")){
		 var options={};
		 	options.count=row.outQty;
			options.goodsNo=row.goodsNo;
			options.batchNo=row.batchNo;
			options.validate=row.validate;
			createQRBarCode(options);
		}
}
 </script>
</body>
</html>